package model

import (
	"fmt"
	"gorm.io/gorm"
	"project_file_manager/conn"
)

//在 Go 项目中优雅的使用 gorm v2 - SegmentFault 思否 https://segmentfault.com/a/1190000039097157

type FileInfo struct {
	gorm.Model
	Name string `gorm:"not null;"`
	Path string `gorm:"not null;"`
}

func init() {
	conn.SetupTableModel(&FileInfo{})
	_ = fmt.Errorf("已为FileInfo建表")
}

func (fi *FileInfo) Save() {
	conn.GetDB().Create(fi)
	fmt.Println("saved :" + fi.Path)
}
func (fi *FileInfo) GetAll() (infos []FileInfo, e error) {
	infos = make([]FileInfo, 0)

	result := conn.GetDB().Find(&infos)
	// SELECT * FROM fileInfos;
	r := result.RowsAffected // 返回找到的记录数，相当于 `len(infos)`
	e = result.Error         // returns error
	fmt.Printf("所有用户数据有%d条 ，err: %v\n", r, e)
	return
}
